package com.g2room.driving.grap.life;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.g2room.driving.grap.life.model.po.GrapErr;
import com.g2room.driving.grap.life.model.po.LifeNews;
import com.g2room.driving.util.DateUtil;
import com.g2room.myuall.util.DbUtil;

public class GenericGrapNews {
	public static String db = "life_grap";
//	public static List<Keyword> newsReplaceKeyword = null;
	protected Connection conn = null;
	public static List<String> soruceUrls = new ArrayList<String>();
	
	static {
		JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
	}
	
	public void insert2NewsDb(List<LifeNews> nis) {
		String sql = "insert into sch_news(author,title,summary,content,publish_time,school_name,school_id,type_name,create_time,cover_height,cover_width,cover,id,type_id) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getConn(db).prepareStatement(sql);
			for(LifeNews ni : nis) {
				ps.setString(1, ni.getAuthor());
				ps.setString(2, ni.getTitle());
				ps.setString(3, ni.getSummary());
				ps.setString(4, ni.getContent());
				ps.setString(5, null == ni.getPublishTime() ? DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss") : DateUtil.date2String(ni.getPublishTime(), "yyyy-MM-dd HH:mm:ss"));
	
				ps.setString(6, ni.getSchoolName());
				ps.setInt(7, ni.getSchoolId());
				ps.setString(8, ni.getTypeName());
				ps.setString(9, DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss"));
				ps.setInt(10, ni.getCoverHeight());
				ps.setInt(11, ni.getCoverWidth());
				ps.setString(12, ni.getCover());
				ps.setInt(13, ni.getId());
				ps.setInt(14, ni.getTypeId());
				ps.addBatch();
			}
			ps.executeBatch();
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new GrapErr();
			ge.setType("5");
			ge.setContent(JSON.toJSONString(nis));
			ge.setDescription("insert DB 错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if (null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
				}
			}
		}
	}

	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getConn(db).prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void getSourceUrl(String tb) {
		try {
			Statement statement = DbUtil.getConn(db).createStatement();
			String sql = "select source_url from " + tb;
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				soruceUrls.add(rs.getString("source_url"));
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 去掉表情
	 * @param str
	 * @return
	 */
	public static String getUnUnicStr(String str) {
		return str.replaceAll("[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]", "");
	}
}
